package com.hhdys.action;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hhdys.annotation.Access;
import com.hhdys.service.DepartmentService;

@Controller
@RequestMapping("/admin/department")
@Access("部门管理")
public class DepartmentAction {
	@Autowired
	private DepartmentService departmentService;

	@RequestMapping("/departmentList")
	@Access("查看")
	public String showDepartments(Model model) {
		model.addAttribute("json", departmentService.getDepartmentsJson());
		return "admin/department/departmentList";
	}

	@RequestMapping("/del")
	@ResponseBody
	@Access("删除")
	public String delDepartment(int id) {
		if (departmentService.delDepartment(id)) {
			return "success";
		}
		return "error";
	}

	@RequestMapping("/update")
	@ResponseBody
	@Access("修改")
	public String editDepartment(int id, String name) {
		Subject curUser = SecurityUtils.getSubject();
		if (departmentService.EditDepartment(id, name,
				(String) curUser.getPrincipal())) {
			return "success";
		}
		return "error";
	}

	@RequestMapping("/add")
	@ResponseBody
	@Access("添加")
	public String addDepartment(int parentId, String name) {
		Subject curUser = SecurityUtils.getSubject();
		if (departmentService.addDeparment(parentId, name,
				(String) curUser.getPrincipal())) {
			return "success";
		}
		return "error";
	}

	/**
	 * @return the departmentService
	 */
	public DepartmentService getDepartmentService() {
		return departmentService;
	}

	/**
	 * @param departmentService
	 *            the departmentService to set
	 */
	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}
}
